package com.zhoug.imageloader3;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

import java.security.MessageDigest;

import androidx.annotation.NonNull;

/**
 * Glide给图片加水印
 *
 * @Author: zhoug
 * @Date: 2023/7/11
 * @Description:
 */
public class WatermarkTransformation extends BitmapTransformation {
    private Paint mPaint;
    private String watermark;//水印

    public WatermarkTransformation(String watermark) {
        this.watermark = watermark;
        mPaint = new Paint();
        mPaint.setColor(Color.BLACK);
        mPaint.setTextSize(50);
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setFilterBitmap(true);
    }


    @Override
    protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
        if (watermark != null && watermark.length() > 0) {
            return addWatermark(toTransform);
        }
        return toTransform;
    }


    @Override
    public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
    }

    private Bitmap addWatermark(Bitmap oldBitmap) {
        int width = oldBitmap.getWidth();
        int height = oldBitmap.getHeight();
        Canvas canvas = new Canvas(oldBitmap);
        canvas.drawText(watermark, 10, height - 100, mPaint);
        return oldBitmap;
    }

}
